package com.atguigu.spzx.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.atguigu.spzx.model.dto.user.LoginDto;
import com.atguigu.spzx.model.dto.user.UserRegisterDto;
import com.atguigu.spzx.model.pojo.h5.UserInfo;
import com.atguigu.spzx.model.pojo.h5.UserInfoAuthContextUtil;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.user.mapper.UserInfoMapper;
import com.atguigu.spzx.user.service.UserInfoService;
import com.atguigu.spzx.utils.DYLAssert;
import com.atguigu.spzx.utils.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean doRegister(UserRegisterDto userRegisterDto) {
        String username = userRegisterDto.getUsername();
        String nickName = userRegisterDto.getNickName();
        String password = userRegisterDto.getPassword();
        String code = userRegisterDto.getCode();
        // 校验验证码
        String s = redisTemplate.opsForValue().get("phone:code:" + username);
        DYLAssert.isTrue(StringUtils.isNotEmpty(s), "验证码错误!");
        DYLAssert.isTrue(s.equals(code), "验证码错误！");

        // 手机号或用户名是否已经注册
        UserInfo user = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("username", username));
        DYLAssert.isNull(user, "注册失败，用户名已经被注册！");

        UserInfo userInfo = new UserInfo();
        // 密码加密
        String encodePassword = MD5Util.encode(password);
        userInfo.setPassword(encodePassword);
        userInfo.setUsername(username);
        userInfo.setAvatar("http://localhost:9000/spzx-bucket/avatar/2025/01/21/th.jpg");
        //KEYPOINT username就是手机号
        userInfo.setPhone(username);
        userInfo.setNickName(nickName);
        userInfo.setStatus(1);
        int insert = userInfoMapper.insert(userInfo);
        return insert == 1;
    }

    /*
     * @description: 用户登录
     **/
    @Override
    public String doLogin(LoginDto loginRequest) {
        String password = loginRequest.getPassword();
        String userName = loginRequest.getUsername();
        //  校验用户名密码是否匹配
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("username", userName));
        DYLAssert.notNull(userInfo, "用户名或密码错误！");
        DYLAssert.isTrue(MD5Util.encode(password).equals(userInfo.getPassword()), "用户名或密码错误！");
        // 生成token
        String token = UUID.randomUUID().toString().replace("-", "");
        String jsonString = JSON.toJSONString(userInfo, SerializerFeature.BrowserSecure);
        // 用户存入redis,设置过期时间
        redisTemplate.opsForValue().set("user:login:" + token, jsonString, 6, TimeUnit.HOURS);
        return token;
    }

    @Override
    public UserInfo getUserByToken(String token) {
        // 从redis中获取sysUser
        String s = redisTemplate.opsForValue().get("user:login:" + token);
        return JSON.parseObject(s, UserInfo.class);
    }

    @Override
    public UserInfoVo getCurrentUserInfo() {
        UserInfo userInfo = UserInfoAuthContextUtil.get();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(userInfo.getNickName());
        userInfoVo.setAvatar(userInfo.getAvatar());
        return userInfoVo;
    }
}
